﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace NS
{
    [HotUpdate]
    [Description("物料修改单价后更新采购申请单（未关闭）")]
    public class ModifyUnitPrice : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.PreparePropertysEventArgs e)
        {
            e.FieldKeys.Add("FMATERIALID");
            base.OnPreparePropertys(e);
        }

        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);
            string FMATERIALID = "";
            foreach (DynamicObject item in e.DataEntitys)
            {
                if (item["ID"] != null && Convert.ToString(item["ID"]) != "")
                {
                    FMATERIALID = Convert.ToString(item["ID"]);
                }
                string SQL = "/*dialect*/" + string.Format(@"update t set t.F_WNEG_PRICE1=t1.F_QZPV_PRICE,
                t.F_WNEG_PRICE=t1.F_QZPV_PRICE1,
                t.F_WNEG_Base=t3.F_WLLZ_TAXRATEID from T_PUR_ReqEntry t
                left join T_BD_MATERIAL t1 on t.FMATERIALID=t1.FMATERIALID
                left join T_BD_MATERIALBASE t3 on t1.FMATERIALID=t3.FMATERIALID
                left join T_PUR_Requisition t2 on t.fid=t2.fid
                where t2.FCLOSESTATUS='A' AND t1.FMATERIALID='{0}'", FMATERIALID);
                DBUtils.ExecuteDynamicObject(this.Context, SQL);
            }
            // 设置操作完成后显示操作结果

            this.OperationResult.IsShowMessage = true;

            // 输出一条成功消息

            var result = new OperateResult();

            result.SuccessStatus = true;

            result.PKValue = e.DataEntitys[0]["Id"];

            result.Number = ObjectUtils.Object2String(this.BusinessInfo.GetBillNoField().DynamicProperty.GetValueFast(e.DataEntitys[0]));

            result.Message = string.Format("更新含税价/出厂价/结算税率成功！请查看采购申请单");

            this.OperationResult.OperateResult.Add(result);
        }
    }
}
